package aor2.cpu.blockinterrupt;

import aor2.components.Restorable;
import aor2.components.SignalSet;

public class InterruptSignalSet extends SignalSet implements Restorable {

	private static InterruptSignalSet singleton = null;

	public static InterruptSignalSet getInstance() {
		if (null == singleton)
			singleton = new InterruptSignalSet();
		return singleton;
	}

	private InterruptSignalSet() {
		add("inm");
		add("notintr1", true);
		add("notintr2", true);
		add("notintr3", true);

		// SVE ZA ExternInterruptBoard
		add("PINTR1");
		add("PRIRR1");
		add("PINTR2");
		add("PRIRR2");
		add("PINTR3");
		add("PRIRR3");
		add("PINM");
		add("STINM");

		add("SINTR1");
		add("STINTR1");
		add("SINTR2");
		add("STINTR2");
		add("SINTR3");
		add("STINTR3");

		add("intr1");
		add("intr2");
		add("intr3");

		add("CLRINTR1");
		add("CLRINTR2");
		add("CLRINTR3");
		// ovi su u condition signal set
		// add("PRINS");
		// add("PRCOD", InstructionSignal.UNKNOWN.getSignal());
		// add("PRADR");
		// add("PRINM");
		// add("PRIMM");
	}

	@Override
	public void restore(String restorePoint) {
		String trim = restorePoint.replace(beginTag(), "");
		trim = trim.replace(endTag(), "");
		trim = trim.trim();
		String[] states = trim.split(" ");
		int i = 0;

		get("inm").clearHistory();
		get("notintr1").clearHistory();
		get("notintr2").clearHistory();
		get("notintr3").clearHistory();

		// SVE ZA ExternInterruptBoard
		get("PINTR1").clearHistory();
		get("PRIRR1").clearHistory();
		get("PINTR2").clearHistory();
		get("PRIRR2").clearHistory();
		get("PINTR3").clearHistory();
		get("PRIRR3").clearHistory();
		get("PINM").clearHistory();
		get("STINM").clearHistory();

		get("SINTR1").clearHistory();
		get("STINTR1").clearHistory();
		get("SINTR2").clearHistory();
		get("STINTR2").clearHistory();
		get("SINTR3").clearHistory();
		get("STINTR3").clearHistory();

		get("intr1").clearHistory();
		get("intr2").clearHistory();
		get("intr3").clearHistory();

		get("CLRINTR1").clearHistory();
		get("CLRINTR2").clearHistory();
		get("CLRINTR3").clearHistory();

		// --------------------------------------------------------------
		// --------------------------------------------------------------
		// --------------------------------------------------------------

		get("inm").setActive(Boolean.parseBoolean(states[i++]));
		get("notintr1").setActive(Boolean.parseBoolean(states[i++]));
		get("notintr2").setActive(Boolean.parseBoolean(states[i++]));
		get("notintr3").setActive(Boolean.parseBoolean(states[i++]));

		// SVE ZA ExternInterruptBoard
		get("PINTR1").setActive(Boolean.parseBoolean(states[i++]));
		get("PRIRR1").setActive(Boolean.parseBoolean(states[i++]));
		get("PINTR2").setActive(Boolean.parseBoolean(states[i++]));
		get("PRIRR2").setActive(Boolean.parseBoolean(states[i++]));
		get("PINTR3").setActive(Boolean.parseBoolean(states[i++]));
		get("PRIRR3").setActive(Boolean.parseBoolean(states[i++]));
		get("PINM").setActive(Boolean.parseBoolean(states[i++]));
		get("STINM").setActive(Boolean.parseBoolean(states[i++]));

		get("SINTR1").setActive(Boolean.parseBoolean(states[i++]));
		get("STINTR1").setActive(Boolean.parseBoolean(states[i++]));
		get("SINTR2").setActive(Boolean.parseBoolean(states[i++]));
		get("STINTR2").setActive(Boolean.parseBoolean(states[i++]));
		get("SINTR3").setActive(Boolean.parseBoolean(states[i++]));
		get("STINTR3").setActive(Boolean.parseBoolean(states[i++]));

		get("intr1").setActive(Boolean.parseBoolean(states[i++]));
		get("intr2").setActive(Boolean.parseBoolean(states[i++]));
		get("intr3").setActive(Boolean.parseBoolean(states[i++]));

		get("CLRINTR1").setActive(Boolean.parseBoolean(states[i++]));
		get("CLRINTR2").setActive(Boolean.parseBoolean(states[i++]));
		get("CLRINTR3").setActive(Boolean.parseBoolean(states[i++]));

	}

	@Override
	public String createRestorePoint() {
		return beginTag()
				+ "\n\t"
				+ get("inm").isActive()
				+ " "
				+ get("notintr1").isActive()
				+ " "
				+ get("notintr2").isActive()
				+ " "
				+ get("notintr3").isActive()
				+ " "
				+

				// SVE ZA ExternInterruptBoard
				get("PINTR1").isActive() + " " + get("PRIRR1").isActive() + " "
				+ get("PINTR2").isActive() + " " + get("PRIRR2").isActive()
				+ " " + get("PINTR3").isActive() + " "
				+ get("PRIRR3").isActive() + " " + get("PINM").isActive() + " "
				+ get("STINM").isActive() + " " +

				get("SINTR1").isActive() + " " + get("STINTR1").isActive()
				+ " " + get("SINTR2").isActive() + " "
				+ get("STINTR2").isActive() + " " + get("SINTR3").isActive()
				+ " " + get("STINTR3").isActive() + " " +

				get("intr1").isActive() + " " + get("intr2").isActive() + " "
				+ get("intr3").isActive() + " " +

				get("CLRINTR1").isActive() + " " + get("CLRINTR2").isActive()
				+ " " + get("CLRINTR3").isActive() + "\n" + endTag();
	}

	@Override
	public String beginTag() {
		return "<InterruptSignalSet>";
	}

	@Override
	public String endTag() {
		return "</InterruptSignalSet>";
	}

	public static void main(String[] args) {
		InterruptSignalSet iss = InterruptSignalSet.getInstance();

		String res = iss.createRestorePoint();
		System.out.println(res);

		iss.restore(res);

		res = iss.createRestorePoint();
		System.out.println(res);

	}
}
